A stack is a list in which insertions and deletions take place at the same end. This end is called the top. The other end of the list is called the bottom. It is also called a LIFO(last-in-first-out) list. 栈是一个插入和删除都发生在最后面的链表,这个最后面被我们称作top,而链表的另一边被我们称作bottom.这个链表被我们成为LIFO(后进先出)列表
理论上有方法:push,pop
1.1. 栈的模型
1 2 3 4 5 6 7 8 9 10 11 12 13
AbstractDataType Stack{ instances list of elements; one end is called the bottom; the other is the top; operations Create():Create an empty stack; IsEmpty():Return trueif stack is empty,returnfalseotherwise IsFull():Return trueif stack if full,returnfalse otherwise; Top():return top element of the stack; Add(x): add element x to the stack; Delete(x):Delete top element from stack and put it in x; }
//c++ #include<iostream.h> #include<string.h> #include<stdio.h> #include"stack.h" constint Maxlength = 100; // max expression length usingnamespace std; voidPrintMatchedPairs(char *expr){ Stack<int> s(Maxlength); int j, length = strlen(expr); for ( int i = l; i <= length; i++) { if (expr[i-1]=="(") s.Add(i); elseif (expr[i-1]==")") try { s.Delete(j);//进栈的是括号的位置 cout << j <<" "<<i<< endl;} catch (OutOfBounds) {cout << "No match for right parenthesis" << "at"<< i << endl;} } while ( !s.IsEmpty ()){ s.Delete(j); cout<< "No match for left parenthesis at " << j << endl; } } voidstaticmain(void){ char expr[MaxLength]; cout<< "type an expression of length at most" <<MaxLength<<endl; cin.getline(expr, MaxLength); cout<<"the pairs of matching parentheses in"<<endl; puts(expr); cout<<"are"<<endl; printMatcnedPairs(expr); } //复杂度O(n)
voidpostfix(expression e) { Stack <char> s; char ch, y ; s.MakeEmpty (); s.Push ("#"); while (cin.get ( ch ) , ch != "#") { if (isdigit ( ch )) cout << ch; elseif (ch ==")") for (y=s.Pop();y!="(";y=s.Pop()) cout << y ; else { for (y = s.Pop ();isp(y)>icp (ch);y=s.Pop( )) cout<<y; s.Push (y); s.Push (ch); } } while (!s.IsEmpty ()){ y = s.Pop (); cout <<y ; } }
2. 队列
两头开,从一边进入,从一边出。
A queue is a linear list in which additions and deletions take place at different ends. It is also called a first-in-first-out list. The end at which new elements are added is called the rear. The end from which old elements are deleted is called the front. 一个队列是一个添加和删除在不同端发生的线性链表,这个队列
2.1. Queue Model
1 2 3 4 5 6 7 8 9 10 11
AbstractDataType Queue { instances ordered list of elements;one end is called the front; the other is the rear; operations Create(): Create an empty queue; IsEmpty(): Return trueif queue is empty,returnfalse otherwise; IsFull(): returntrueif queue is full, returnfalse otherwise; First(): return first element of the queue; Last(): return last element of the queue; Add(x): add element x to the queue; Delete(x): delete front element from the queue and put it in x; }
int NumOfNbrs = 4; Position here, nbr; here.row = start.row; here.col = start.col;//将当前位置置为开始的位置 grid[start.row][start.col] = 2;
LinkedQueue<Position>Q; do{ //label neighbors of here for ( int i = 0; i< NumOfNbrs; i++) { nbr.row = here.row + offset[i].row; nbr.col = here.col + offset[i].col;//给相邻的格子标号,当前格子出对,周围的格子进队 if (grid[nbr.row][nbr.col] == 0) { grid[nbr.row][nbr.col] = grid[here.row][here.col] + 1; if ((nbr.row == finish.row) &&(nbr.col == finish.col)) break; Q.Add(nbr);//周围的格子进队 }// end of if }// end of for if (( nbr.row == finish.row) && (nbr.col == finish.col)) break; if (Q.IsEmpty()) returnfalse; Q.Delete(here); } while(true);
PathLen = grid[finish.row][finish.col]-2; path = new Position [PathLen]; //trace backwards from finish here = finish; for ( int j = PathLen-1; j >= 0; j--) { path[j] = here;//当前的结尾问题,四方向往回找 for ( int i = 0; i < NumOfNbrs; i++) { nbr.row = here.row + offset[i].row; nbr.col = here.col + offset[i].col; if ( grid[nbr.row][nbr.col] == j+2) break; } here = nbr; } returntrue; }
评论